package com.fr.privilege.providers.ldap;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.privilege.authority.Authority;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;

public class LDAPUtils
{
  public static void closeContext(LdapContext paramLdapContext)
  {
    try
    {
      if (paramLdapContext != null)
        paramLdapContext.close();
    }
    catch (NamingException localNamingException)
    {
      FRContext.getLogger().warning(localNamingException.getMessage());
    }
  }

  public static Authority[] retrievalAuthority(String paramString, LDAPAuthenticationProvider paramLDAPAuthenticationProvider, LdapContext paramLdapContext)
  {
    Authority[] arrayOfAuthority = null;
    ArrayList localArrayList = new ArrayList();
    String str1 = paramLDAPAuthenticationProvider.getSearchBase();
    String str2 = paramLDAPAuthenticationProvider.getRoleAttribute();
    str2 = (StringUtils.isBlank(str2)) ? "memberOf" : str2;
    if (paramLdapContext != null)
    {
      label306: int i;
      if ((!(StringUtils.isBlank(paramString))) && (!(StringUtils.isBlank(paramLDAPAuthenticationProvider.getPrincipalSuffix()))))
        paramString = paramString + paramLDAPAuthenticationProvider.getPrincipalSuffix();
      SearchControls localSearchControls = new SearchControls();
      localSearchControls.setSearchScope(2);
      localSearchControls.setReturningAttributes(new String[] { str2 });
      String str3 = "(&(objectClass=user)(userPrincipalName=" + paramString + "))";
      try
      {
        NamingEnumeration localNamingEnumeration1 = paramLdapContext.search(str1, str3, localSearchControls);
        if ((localNamingEnumeration1 != null) && (localNamingEnumeration1.hasMoreElements()))
        {
          Attribute localAttribute;
          SearchResult localSearchResult = (SearchResult)localNamingEnumeration1.nextElement();
          Attributes localAttributes = localSearchResult.getAttributes();
          if (localAttributes == null)
          {
            localNamingEnumeration2 = null;
            return localNamingEnumeration2;
          }
          NamingEnumeration localNamingEnumeration2 = localAttributes.getAll();
          do
          {
            if (!(localNamingEnumeration2.hasMoreElements()))
              break label306;
            localAttribute = (Attribute)localNamingEnumeration2.nextElement();
          }
          while (!(localAttribute.getID().equals(str2)));
          NamingEnumeration localNamingEnumeration3 = localAttribute.getAll();
          while (true)
          {
            if (!(localNamingEnumeration3.hasMoreElements()));
            String[] arrayOfString = ((String)localNamingEnumeration3.nextElement()).split(",");
            String str4 = arrayOfString[0].substring(3);
            localArrayList.add(str4);
          }
        }
        arrayOfAuthority = new Authority[localArrayList.size()];
        for (i = 0; i < arrayOfAuthority.length; ++i)
          arrayOfAuthority[i] = new Authority(localArrayList.get(i).toString());
      }
      catch (NamingException localNamingException)
      {
        FRContext.getLogger().log(Level.WARNING, localNamingException.getMessage());
      }
      finally
      {
        closeContext(paramLdapContext);
      }
    }
    return arrayOfAuthority;
  }
}